//
//  MOLaderEvaluationListViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/12.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOLaderEvaluationListViewController.h"
#import "MOLaderEvaluationListCell.h"
#import "MOLaderEvaluationDetailViewController.h"
#import "MOWorkTaskSelectdTaskTypeView.h"
#import "MOWorkTaskSelectdTaskTypeModel.h"

@interface MOLaderEvaluationListViewController ()<UITableViewDelegate,UITableViewDataSource,XLsn0wPickerTimerDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)UIView *headerView;
//选择时间
@property(nonatomic,strong)UIButton *selectedDateButton;
//选择季度
@property(nonatomic,strong)UIButton *selectedTaskTypeButton;
//选择的数据数组
@property(nonatomic,strong)NSMutableArray *selectTaskDataAray;
//选择的model
@property(nonatomic,strong)MOWorkTaskSelectdTaskTypeModel *selectedModel;
//时间
@property(nonatomic,strong)NSString *dateStr;
//季度
@property(nonatomic,strong)NSString *jiDuStr;

@end

@implementation MOLaderEvaluationListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNav];
    
    [self creatView];
    
    [self initData];
    
    [self getData];
}

-(void)initNav
{
//    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
//    [addButton setImage:IMAGE(@"use_add") forState:UIControlStateNormal];
//    [addButton addTarget:self action:@selector(clickAddTask) forControlEvents:UIControlEventTouchUpInside];
//
//    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:addButton];
//    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

-(void)creatView
{
    self.title = @"领导评鉴";
    
    self.view.backgroundColor = KGrayColor_ededed;
    
    [self.view addSubview:self.headerView];
    
    [self.view addSubview:self.tableView];
}

-(void)initData
{
    self.dateStr = [NSString getCurrentTimesAndFormat:@"YYYY"];
    NSString *jidu = @"";
    if ([[NSString getJiDu] integerValue]==1) {
        jidu = @"Q01";
    }else if ([[NSString getJiDu] integerValue]==2){
        jidu = @"Q02";
    }else if ([[NSString getJiDu] integerValue]==3){
        jidu = @"Q03";
    }else{
        jidu = @"Q04";
    }
    self.jiDuStr = jidu;
}

-(void)getData
{
    NSString *url = [NSString stringWithFormat:@"%@?nd=%@&jd=%@&authorization=%@",SZRSLDPJ_QUERYLDPJLIST_URL,self.dateStr,self.jiDuStr,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{} success:^(id responseObject) {
        NSLog(@"领导评鉴：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray *dataArray = responseObject[@"data"];
                self.dataSource = [NSMutableArray array];
                for (int i = 0; i<dataArray.count; i++) {
                    NSDictionary *dataDic = dataArray[i];
                    MOLaderEvaluationListModel *model = [[MOLaderEvaluationListModel alloc]init];
                    model.nameStr = [NSString passNullForString:dataDic[@"xm"]];
                    model.nameIconStr = @"";
                    switch ([[NSString passNullForString:dataDic[@"pjfz"]] integerValue]) {
                        case 50:
                            model.gradeStr = @"差";
                            break;
                        case 70:
                            model.gradeStr = @"一般";
                            break;
                        case 90:
                            model.gradeStr = @"较好";
                            break;
                        case 100:
                            model.gradeStr = @"好";
                            break;
                            
                        default:
                            break;
                    }
                    
                    model.companyStr = dataDic[@"dw"];
                    model.sectionStr = dataDic[@"bm"];
                    model.dutyStr = dataDic[@"zw"];
                    model.peopleCategoryStr = dataDic[@"lb"];
                    model.fenZhiStr = dataDic[@"pjfz"];
                    model.idStr = dataDic[@"oidstr"];
                    [self.dataSource addObject:model];
                }
                [self.tableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        NSLog(@"领导评鉴：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];

}

#pragma mark -- 选择任务类型
-(void)clickSelectedTaskTypeButton:(UIButton *)sender
{
    if (sender.tag==0) {
        
        XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
        pickerDate.maxComponentCount = 1;
        pickerDate.xlsn0wDelegate = self;
        [pickerDate show];
        
    }else{
//        MOWorkTaskSelectdTaskTypeView *view = [[MOWorkTaskSelectdTaskTypeView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//        __weak typeof (self)weaakSelf = self;
//        view.clickSaveButtonBlock = ^(MOWorkTaskSelectdTaskTypeModel * _Nonnull model) {
//            weaakSelf.selectedModel = model;
//            [weaakSelf.selectedTaskTypeButton setTitle:model.nameStr forState:UIControlStateNormal];
//            [weaakSelf.selectedTaskTypeButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
//        };
//        view.clickCancelButtonBlock = ^{
//            weaakSelf.selectedTaskTypeButton.selected = NO;
//        };
//        if (self.selectedModel) {
//            view.currentIdStr = self.selectedModel.idStr;
//        }
//        view.resultArray = self.selectTaskDataAray;
//        [WINDOW addSubview:view];
        
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"第一季度",@"第二季度",@"第三季度",@"第四季度"];
        NSArray *vArr = @[@"Q01",@"Q02",@"Q03",@"Q04"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            [weakSelf.selectedTaskTypeButton setTitle:tArr[index] forState:UIControlStateNormal];
            weakSelf.jiDuStr = vArr[index];
            [weakSelf getData];
        };
        [WINDOW addSubview:pickview];
        
    }
    
}

- (void)pickerTimer:(XLsn0wPickerTimer *)pickerTimer year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    
    NSString *dateStr = [NSString stringWithFormat:@"%ld", (long)year];
    self.dateStr = dateStr;
    [self getData];
    
    self.selectedDateButton.selected = NO;
    [self.selectedDateButton setTitle:dateStr forState:UIControlStateNormal];
    [self.selectedDateButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
    
}

#pragma mark -- 添加任务
-(void)clickAddTask
{
    MOLaderEvaluationDetailViewController *vc = [[MOLaderEvaluationDetailViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOLaderEvaluationListCell *cell = [MOLaderEvaluationListCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50 + 17*5 + 15*5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==self.dataSource.count-1) {
        return 13;
    }else{
        return 0.0001;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 13;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOLaderEvaluationDetailViewController *vc = [[MOLaderEvaluationDetailViewController alloc]init];
    __weak typeof (self)weakSelf = self;
    vc.saveSucessBlock = ^{
        [weakSelf getData];
    };
    vc.edit = @"1";
    vc.dateStr = self.dateStr;
    vc.jiDuStr = self.jiDuStr;
    vc.listModel = self.dataSource[indexPath.section];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(15, self.headerView.bottom, SCREEN_WIDTH - 30, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - 44) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
    
    }
    return _tableView;
}

-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        _headerView.backgroundColor = KWhiteColor;

        _selectedDateButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/2, 44)];
        _selectedDateButton.titleLabel.font = FONT_SIZE(14);
        [_selectedDateButton setTitleColor:KBlackColor_222222 forState:UIControlStateNormal];
        [_selectedDateButton setTitle:[NSString getCurrentTimesAndFormat:@"YYYY"] forState:UIControlStateNormal];
        [_selectedDateButton setImage:IMAGE(@"taskDown") forState:UIControlStateNormal];
        [_selectedDateButton setImage:IMAGE(@"taskTop") forState:UIControlStateSelected];
        [_selectedDateButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
        [_selectedDateButton addTarget:self action:@selector(clickSelectedTaskTypeButton:) forControlEvents:UIControlEventTouchUpInside];
        _selectedDateButton.tag = 0;
        [_headerView addSubview:_selectedDateButton];
        
        NSString *jidu = @"";
        if ([[NSString getJiDu] integerValue]==1) {
            jidu = @"第一季度";
        }else if ([[NSString getJiDu] integerValue]==2){
            jidu = @"第二季度";
        }else if ([[NSString getJiDu] integerValue]==3){
            jidu = @"第三季度";
        }else{
            jidu = @"第四季度";
        }
        _selectedTaskTypeButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, 44)];
        _selectedTaskTypeButton.titleLabel.font = FONT_SIZE(14);
        [_selectedTaskTypeButton setTitleColor:KBlackColor_222222 forState:UIControlStateNormal];
        [_selectedTaskTypeButton setTitle:jidu forState:UIControlStateNormal];
        [_selectedTaskTypeButton setImage:IMAGE(@"taskDown") forState:UIControlStateNormal];
        [_selectedTaskTypeButton setImage:IMAGE(@"taskTop") forState:UIControlStateSelected];
        [_selectedTaskTypeButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
        [_selectedTaskTypeButton addTarget:self action:@selector(clickSelectedTaskTypeButton:) forControlEvents:UIControlEventTouchUpInside];
        _selectedTaskTypeButton.tag = 1;
        [_headerView addSubview:_selectedTaskTypeButton];
        
    }
    return _headerView;
}

@end
